import request from './request'
import {
  DEFAULT_BRANCH,
  USERNAME,
  REPO_NAME,
  createFilePath, getFileName
} from './user-info'


/**
 * 文件上传
 * @param path 文件路径
 * @param sha sha
 * @returns promise Promise
 */
export function delFile(path: string, sha: string) {
  return request.instance({
    url: `/repos/${USERNAME}/${REPO_NAME}/contents/${path}`,
    method: 'DELETE',
    data: {
      sha: sha,
      message: `upload file ${path}`,
      branch: DEFAULT_BRANCH
    }
  })
}


/**
 * 文件上传
 * @param fileName 文件名
 * @param base64code base64 文件编码
 * @returns promise Promise
 */
export function upload(fileName: string, base64code: string) {
  fileName = getFileName(fileName)
  return request.instance({
    url: `/repos/${USERNAME}/${REPO_NAME}/contents/${createFilePath()}/${fileName}`,
    method: 'PUT',
    data: {
      content: base64code,
      message: `upload file ${fileName}`,
      branch: DEFAULT_BRANCH
    }
  })

}
